"""
########################################
文件描述：log文件生成，log输出封装，尚未完成
########################################
"""

import logging
import time
import os
from common.base_path import base_path


# 创建log文件
def logfile():
    '''创建log文件'''
    rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
    log_path = base_path() + '/Logs/'
    # print(log_path)
    log_file = log_path + rq + '.log'
    return log_file


# log输出到文件和运行窗口
def logout(level, content):
    '''
    level：报警级别，debug,info,warning,error,crotical
    '''
    logger = logging.getLogger('mylogger')
    logger.setLevel(logging.INFO)
    if not logger.handlers:
        fh = logging.FileHandler(logfile(), encoding='utf-8')
        sh = logging.StreamHandler()
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        formatter2 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s [line:%(lineno)d] : %(message)s')
        fh.setFormatter(formatter)
        sh.setFormatter(formatter2)
        logger.addHandler(fh)
        logger.addHandler(sh)
    if level == 'debug':
        logger.debug(content)
    elif level == 'info':
        logger.info(content)
    elif level == 'warning':
        logger.warning(content)
    elif level == 'error':
        logger.error(content)
    elif level == 'crotical':
        logger.critical(content)
    # # 在记录日志之后移除句柄
    # logger.removeHandler(fh)
    # logger.removeHandler(sh)


if __name__ == '__main__':

    for i in range(5):
        # print(i)
        logout('error', str(i))
